<template>
  <el-dialog
    :title="!dataForm.gsmGroupId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :append-to-body="true"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      {{dataForm}}
      <el-form-item label="拼团商品" prop="psmId">
        砍价商品 <img :src="goods.psmImg" alt="" style="width: 100px ; height: 100px"> <br>
        {{ goods.psmTitle }} <br>
        原价:{{ goods.psmPrice }} <br>
        库存:{{ goods.psmStock }} <br>
        <el-button type="primary" plain @click="chooseGoods()">选择商品</el-button>
      </el-form-item>
      <el-form-item label="活动封面" prop="gsmGroupImg">
        <el-upload
          class="upload-demo"
          action="http://third-party.snow-mall.com/thirdparty/oss/policy"
          :on-success="handleSuccess"
          list-type="picture"
          :file-list="fileList">
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="活动名称" prop="gsmGroupName">
        <el-input v-model="dataForm.gsmGroupName" placeholder="活动名称"></el-input>
      </el-form-item>
      <el-form-item label="生效时间" prop="gsmGroupStartTime">
        <el-date-picker
          v-model="dataForm.gsmGroupStartTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间">
        </el-date-picker>
        至
        <el-date-picker
          v-model="dataForm.gsmGroupEndTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="拼团库存" prop="gsmGroupStock">
        <el-input v-model="dataForm.gsmGroupStock" placeholder="拼团库存"></el-input>
      </el-form-item>
      <el-form-item label="拼团价格" prop="gsmGroupPrice">
        <el-input v-model="dataForm.gsmGroupPrice" placeholder="拼团价格"></el-input>
      </el-form-item>
      <el-form-item label="成团人数" prop="gsmGroupCount">
        <el-input v-model="dataForm.gsmGroupCount" placeholder="成团人数"></el-input>
      </el-form-item>
      <el-form-item label="成团时效" prop="gsmGroupDuration">
        <el-input v-model="dataForm.gsmGroupDuration" placeholder="成团时效"></el-input>
      </el-form-item>
      <el-form-item label="限购数量" prop="gsmGroupLimitCount">
        <el-input v-model="dataForm.gsmGroupLimitCount" placeholder="限购数量"></el-input>
      </el-form-item>
      <el-form-item label="运费" prop="gsmGroupFreight">
        <el-switch
          v-model="dataForm.gsmGroupFreight"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-value="0"
          inactive-value="1"
          active-text="包邮"
          inactive-text="不包邮">
        </el-switch>
      </el-form-item>
      <el-form-item label="奖金" prop="gsmGroupBonus">
        <el-switch
          v-model="dataForm.gsmGroupBonus"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-value="0"
          inactive-value="1"
          active-text="有返金"
          inactive-text="无返金">
        </el-switch>
      </el-form-item>
      <el-form-item label="分享描述" prop="gsmGroupShareDescription">
        <el-input v-model="dataForm.gsmGroupShareDescription" placeholder="分享描述"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    <el-dialog
      title="商品列表"
      :visible.sync="dialogFormVisible"
      :append-to-body="true">
      <el-table
        ref="singleTable"
        :data="goodsList"
        highlight-current-row
        @current-change="handleCurrentChange"
        style="width: 100%">
        <el-table-column
          type="index"
          width="50">
        </el-table-column>
        <el-table-column
          property="psmCode"
          label="商品编号"
          width="120">
        </el-table-column>
        <el-table-column
          property="psmName"
          label="商品名称"
          width="120">
        </el-table-column>
        <el-table-column
          property="psmTitle"
          label="商品标题"
          width="120">
        </el-table-column>
        <el-table-column
          property="psmImg"
          label="商品展示"
          width="120">
          <template slot-scope="scope">
            <img :src="scope.row.psmImg" alt="" style="width: 100px ; height: 100px">
          </template>
        </el-table-column>
        <el-table-column
          property="psmWeight"
          label="重量"
          width="120">
        </el-table-column>
        <el-table-column
          property="psmBrand"
          label="商品品牌"
          width="120">
        </el-table-column>
        <el-table-column
          property="psmPrice"
          label="商品价格"
          width="120">
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </el-dialog>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      fileList: [],
      dialogFormVisible: false,
      goodsList: [],
      goods: {},
      visible: false,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataForm: {
        gsmGroupId: 0,
        psmId: '',
        gsmGroupImg: '',
        gsmGroupName: '',
        gsmGroupStartTime: '',
        gsmGroupEndTime: '',
        gsmGroupStock: '',
        gsmGroupPrice: '',
        gsmGroupCount: '',
        gsmGroupDuration: '',
        gsmGroupLimitCount: '',
        gsmGroupFreight: '',
        gsmGroupBonus: '',
        gsmGroupShareDescription: '',
        gsmStatus: '',
        psmName:'',
        psmImg:'',
        psmPrice:''
      },
      dataRule: {
        psmId: [
          {required: true, message: '拼团商品不能为空', trigger: 'blur'}
        ],
        gsmGroupImg: [
          {required: true, message: '活动封面不能为空', trigger: 'blur'}
        ],
        gsmGroupName: [
          {required: true, message: '活动名称不能为空', trigger: 'blur'}
        ],
        gsmGroupStartTime: [
          {required: true, message: '生效时间不能为空', trigger: 'blur'}
        ],
        gsmGroupEndTime: [
          {required: true, message: '过期时间不能为空', trigger: 'blur'}
        ],
        gsmGroupStock: [
          {required: true, message: '拼团库存不能为空', trigger: 'blur'}
        ],
        gsmGroupPrice: [
          {required: true, message: '拼团价格不能为空', trigger: 'blur'}
        ],
        gsmGroupCount: [
          {required: true, message: '成团人数不能为空', trigger: 'blur'}
        ],
        gsmGroupDuration: [
          {required: true, message: '成团时效不能为空', trigger: 'blur'}
        ],
        gsmGroupLimitCount: [
          {required: true, message: '限购数量不能为空', trigger: 'blur'}
        ],
        gsmGroupFreight: [
          {required: true, message: '运费不能为空', trigger: 'blur'}
        ],
        gsmGroupBonus: [
          {required: true, message: '奖金不能为空', trigger: 'blur'}
        ],
        gsmGroupShareDescription: [
          {required: true, message: '分享描述不能为空', trigger: 'blur'}
        ],
        gsmStatus: [
          {required: true, message: '拼团状态不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    handleSuccess(response, file, fileList) {
      this.dataForm.gsmGroupImg = response;
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getGoods();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getGoods();
    },
    getGoods() {
      this.$http({
        url: this.$http.adornUrl('/search/goods/esList'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.key
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.goodsList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.goodsList = []
          this.totalPage = 0
        }
      })
    },
    handleCurrentChange(currentRow) {
      if (currentRow != null) {
        this.goods = currentRow
        this.dataForm.psmPrice = currentRow.psmPrice
        this.dataForm.psmId = currentRow.psmId
        this.dataForm.psmName = currentRow.psmName
        this.dataForm.psmImg = currentRow.psmImg
        this.dialogFormVisible = false;
      }
    },
    //打开选择商品的窗口 并且查询出当前已上架的商品
    chooseGoods() {
      //打开对话框
      this.dialogFormVisible = true
      this.getGoods();
    },
    init(id) {
      this.dataForm.gsmGroupId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.gsmGroupId) {
          this.$http({
            url: this.$http.adornUrl(`/group/group/info/${this.dataForm.gsmGroupId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.psmId = data.group.psmId
              this.dataForm.gsmGroupImg = data.group.gsmGroupImg
              this.dataForm.gsmGroupName = data.group.gsmGroupName
              this.dataForm.gsmGroupStartTime = data.group.gsmGroupStartTime
              this.dataForm.gsmGroupEndTime = data.group.gsmGroupEndTime
              this.dataForm.gsmGroupStock = data.group.gsmGroupStock
              this.dataForm.gsmGroupPrice = data.group.gsmGroupPrice
              this.dataForm.gsmGroupCount = data.group.gsmGroupCount
              this.dataForm.gsmGroupDuration = data.group.gsmGroupDuration
              this.dataForm.gsmGroupLimitCount = data.group.gsmGroupLimitCount
              this.dataForm.gsmGroupFreight = data.group.gsmGroupFreight
              this.dataForm.gsmGroupBonus = data.group.gsmGroupBonus
              this.dataForm.gsmGroupShareDescription = data.group.gsmGroupShareDescription
              this.dataForm.gsmStatus = data.group.gsmStatus
              this.dataForm.psmName = data.group.psmName
              this.dataForm.psmImg = data.group.psmImg
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/group/group/${!this.dataForm.gsmGroupId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'gsmGroupId': this.dataForm.gsmGroupId || undefined,
              'psmId': this.dataForm.psmId,
              'gsmGroupImg': this.dataForm.gsmGroupImg,
              'gsmGroupName': this.dataForm.gsmGroupName,
              'gsmGroupStartTime': this.dataForm.gsmGroupStartTime,
              'gsmGroupEndTime': this.dataForm.gsmGroupEndTime,
              'gsmGroupStock': this.dataForm.gsmGroupStock,
              'gsmGroupPrice': this.dataForm.gsmGroupPrice,
              'gsmGroupCount': this.dataForm.gsmGroupCount,
              'gsmGroupDuration': this.dataForm.gsmGroupDuration,
              'gsmGroupLimitCount': this.dataForm.gsmGroupLimitCount,
              'gsmGroupFreight': this.dataForm.gsmGroupFreight,
              'gsmGroupBonus': this.dataForm.gsmGroupBonus,
              'gsmGroupShareDescription': this.dataForm.gsmGroupShareDescription,
              'gsmStatus': this.dataForm.gsmStatus,
              'psmName':this.dataForm.psmName,
              'psmImg':this.dataForm.psmImg
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
